package yourapp.expense.dao;

import yourapp.expense.entity.PurposeCategory;
import yourapp.expense.entity.PaymentAppCategory;
import yourapp.expense.entity.ShoppingAppCategory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CategoryDAO {

  private Connection connection;

  public CategoryDAO() {
    // 使用 DatabaseConnection 单例获取数据库连接
    this.connection = DatabaseConnection.getInstance().getConnection();
  }

  /**
   * 获取所有用途分类
   * @return 包含所有用途分类的列表
   */
  public List<PurposeCategory> getAllPurposeCategories() {
    List<PurposeCategory> categories = new ArrayList<>();
    String sql = "SELECT * FROM PurposeCategory";

    try (Statement stmt = connection.createStatement();
         ResultSet rs = stmt.executeQuery(sql)) {

      while (rs.next()) {
        int id = rs.getInt("purpose_category_id");
        String name = rs.getString("name");
        categories.add(new PurposeCategory(id, name));
      }
    } catch (SQLException e) {
      System.err.println("获取用途分类失败: " + e.getMessage());
      e.printStackTrace();
    }

    return categories;
  }

  /**
   * 获取所有支付软件分类
   * @return 包含所有支付软件分类的列表
   */
  public List<PaymentAppCategory> getAllPaymentAppCategories() {
    List<PaymentAppCategory> categories = new ArrayList<>();
    String sql = "SELECT * FROM PaymentAppCategory";

    try (Statement stmt = connection.createStatement();
         ResultSet rs = stmt.executeQuery(sql)) {

      while (rs.next()) {
        int id = rs.getInt("payment_app_category_id");
        String name = rs.getString("name");
        categories.add(new PaymentAppCategory(id, name));
      }
    } catch (SQLException e) {
      System.err.println("获取支付软件分类失败: " + e.getMessage());
      e.printStackTrace();
    }

    return categories;
  }
  /**
   * 获取所有购物软件分类
   * @return 包含所有购物软件分类的列表
   */
  public List<ShoppingAppCategory> getAllShoppingAppCategories() {
    List<ShoppingAppCategory> categories = new ArrayList<>();
    String sql = "SELECT * FROM ShoppingAppCategory";

    try (Statement stmt = connection.createStatement();
         ResultSet rs = stmt.executeQuery(sql)) {

      while (rs.next()) {
        int id = rs.getInt("shopping_app_category_id");
        String name = rs.getString("name");
        categories.add(new ShoppingAppCategory(id, name));
      }
    } catch (SQLException e) {
      System.err.println("获取购物软件分类失败: " + e.getMessage());
      e.printStackTrace();
    }

    return categories;
  }
}